package cn.gwm.flink.streaming.dwm.charge.template;

import cn.gwm.flink.streaming.constant.BaseFields;
import cn.gwm.flink.streaming.dwm.charge.bean.ChargeArgs;
import cn.gwm.flink.streaming.dwm.charge.bean.ChargeFavourable;
import cn.gwm.flink.streaming.dwm.charge.model.ChargeConstant;
import cn.gwm.flink.streaming.dwm.charge.model.ChargeStateEnum;
import cn.gwm.flink.streaming.dwm.charge.model.ChargeTypeEnum;
import cn.gwm.flink.streaming.dwm.charge.support.ChargeFieldSupport;
import cn.gwm.flink.streaming.dwm.charge.support.ChargeStateSupport;
import cn.hutool.json.JSONObject;
import org.apache.flink.api.common.state.MapState;

/**
 * @ClassName PhysicsChargeState
 * @Description 物理充电
 * @Author LiangGuang
 * @Date 2023/05/11 18:04
 */
public class PhysicsChargeState extends ChargeStateModel {

    @Override
    public void stateStart(ChargeArgs args) throws Exception {
        JSONObject summar = args.getSummarState().value();
        if (summar != null) {
            // 结束前对地点的检查设置
            ChargeFieldSupport.placeMatching(summar, args);

            JSONObject prevData = args.getPrevData();
            // 只需要 补充 汇总 并结束 即可  --> 各波段都有自己的开始和结束方法
            // 结尾赋值
            summar.set("chargeStatus", ChargeStateEnum.END.getCode());
            summar.set("end_chrgConnectTid", prevData.getLong(BaseFields.tid));
            summar.set("effectiveChargeTime", args.getEffectiveChargeTime().value() == null ? 0 : args.getEffectiveChargeTime().value());
            // 再次更新 目标soc
            summar.set("Tbox_BattSOCLim", prevData.getStr(args.getConstant().getTboxBattSoc()));

//            favourable(summar, args.getFavourableMapState());

            // 判断，如果 开始连接时间和开始充电时间是一致的，则不完整
            if (summar.containsKey("startChargeTime")) {
                if (summar.getStr("startChargeTime").equals(summar.getStr("chrgConnectTid"))) {
                    summar.set("complete", 0);
                }
            }
            // 如果此时充电类型还是 待识别，则修改为未知
            if (ChargeTypeEnum.CHARGE_TYPE_TO_BE_IDENTIFIED.getCode().equals(summar.getStr("chargeType"))) {
                summar.set("chargeType", ChargeTypeEnum.CHARGE_TYPE_OTHER.getCode());
            }
            summar.set(END_FLAG, BaseFields.defaultVal);
            // 20ms 延迟
            Thread.sleep(20);
            args.getOut().collect(summar);
            // 清空 其余状态
            args.clear();
        } else {
            // 记录优惠电量信息
            JSONObject realNowData = args.getRealNowData();
            ChargeConstant constant = args.getConstant();
            if (!BaseFields.defaultVal.equals(realNowData.getStr(constant.getRequestTime()))) {
                MapState<Long, ChargeFavourable> favourableMap = args.getFavourableMapState();
                Long requestTime = realNowData.getLong(constant.getRequestTime());
                if (!favourableMap.contains(requestTime)) {
                    ChargeFavourable favourable = new ChargeFavourable()
                            .setDiscountPower(ChargeFieldSupport.getIntVal(realNowData, constant.getDiscountPower()))
                            .setDiscountTime(ChargeFieldSupport.getIntVal(realNowData, constant.getDiscountTime()))
                            .setExpensivePower(ChargeFieldSupport.getIntVal(realNowData, constant.getExpensivePower()))
                            .setExpensiveTime(ChargeFieldSupport.getIntVal(realNowData, constant.getExpensiveTime()));
                    // 此时来了数据，但是  优惠信息也还是空，很奇怪 如果全为空，则不需要存 无意义
                    if (!favourable.isZero()) {
                        JSONObject favourableJson = ChargeStateSupport.createFavourable(realNowData, constant, favourable);
                        args.getOut().collect(favourableJson);
                    }
                    favourableMap.clear();
                    favourableMap.put(requestTime, favourable);
                }
            }


        }
    }

    @Override
    public void stateEnd(ChargeArgs args) throws Exception {
        // nothing to do
    }


    @Override
    protected void stateStartSelfHandle(ChargeArgs args) throws Exception {
    }


    @Override
    protected void stateEndSelfHandle(ChargeArgs args) throws Exception {

    }

}
